#include "../include/pclient_pthread.h"
#include "../include/list_queue.h"
#include "../include/msg.h"
#include "../include/mysqlite.h"
#include "../include/device_pthread.h"
#include "../include/myjson.h"

#define JSONBUFSIZE 1024

int pclient_msg_handle(list_t *clinode, msg_t *clientmsg)
{
    int ret;
    printf("开启循环等待处理\n");
    // 申请空间用于存储接收到的json字符串
    char *jsonbuf = (char *)malloc(JSONBUFSIZE);
    msg_queue_t devmsgq;

    while (1)
    {
        memset(jsonbuf, 0, JSONBUFSIZE);
        memset(&devmsgq, 0, sizeof(msg_queue_t));

        ret = recv(clinode->client_sockfd, jsonbuf, JSONBUFSIZE, 0);
        if (ret == -1)
        {
            INFO_LOG("pclient recv error");
            goto err;
        }
        else if (ret == 0)
        {
            INFO_LOG("pclient close");
            goto err;
        }

        // 将json解析到结构体中
        if (json_to_struct(jsonbuf, clientmsg) == -1)
        {
            INFO_LOG("recv data is not json");
            continue;
        }

        if (clientmsg->device_ctrl != 0)
        {
            // 将消息插入到消息队列中 不需要在乎消息的内容是什么，由设备端区分
            memset(devmsgq.jsonbuf, 0, sizeof(devmsgq.jsonbuf));
            strcpy(devmsgq.jsonbuf, jsonbuf);

            printf("pclinent1 send %ld %s\n", devmsgq.msgtype, devmsgq.jsonbuf);
            for (int i = 0; i < strlen(clientmsg->device_id); i++)
            {
                devmsgq.msgtype += clientmsg->device_id[i];
            }
            printf("pclinent2 send %ld %s\n", devmsgq.msgtype, devmsgq.jsonbuf);
            ret = msg_send_to_queue(get_qtod_queue(), &devmsgq);
            if (ret != 0)
            {
                goto err;
            }
        }

        // 在这里阻塞等待dev_to_qt的消息队列中有对应的消息 根据设备号来确定
        memset(devmsgq.jsonbuf, 0, sizeof(devmsgq.jsonbuf));
        ret = msg_recv_to_queue(get_dtoq_queue(), &devmsgq, devmsgq.msgtype);
        if(ret == -1)
        {
            ERR_LOG("msg_recv_to_queue error");
        }

        // 不需要解析，直接将json发送给设备
        printf("pclinent recv %ld %s\n", devmsgq.msgtype, devmsgq.jsonbuf);

        ret = send(clinode->client_sockfd, devmsgq.jsonbuf, strlen(devmsgq.jsonbuf), 0);
        if (ret <= 0)
        {
            ERR_LOG("send to dev error");
        }
    }

    free(jsonbuf);
    return 0;

err:
    free(jsonbuf);
    return -1;
}

/**
 * 用于判断接收到的消息是登录消息还是注册消息
 * 0 成功  -1 失败 需要再次接收登录消息；
 */
int pcclient_login_regist_manage(list_t *clinode, msg_t *clientmsg)
{
    int ret;
    clientmsg->flags = 1;
    char send_json_buf[128] = {0};
    // 先判断是登录请求还是注册请求
    if (clientmsg->cmd == PCCLIENT_LOGIN) // 登录
    {
        // 查询数据库中是否有对应的数据
        ret = mysqlite_query_id_ps(clientmsg->ID, clientmsg->PS);
        if (ret != 0)
        {
            clientmsg->flags = 0;
            set_infomsg_err("not find id or ps");
            goto send_json;
        }

        // 遍历链表中是否有对应的的节点存在，有则已经登录成功，拒绝登录
        int verify = 0;
        for (int i = 0; i < strlen(clientmsg->ID); i++)
            clinode->verify += clientmsg->ID[i];
        for (int i = 0; i < strlen(clientmsg->PS); i++)
            clinode->verify += clientmsg->PS[i];

        ret = list_find_verify(get_pcclient_list_head(), clinode->verify);
        if (ret == 0)
        {
            clientmsg->flags = 0;
            set_infomsg_err("The user has already logged in.");
        }
    }
    else if (clientmsg->cmd == PCCLIENT_REGISTER) // 注册
    {
        ret = mysqlite_add(clientmsg->ID, clientmsg->PS, clientmsg->device_id);
        if (ret != 0)
        {
            clientmsg->flags = 0;
            set_infomsg_err("id and ps  is already exist");
        }
    }

send_json:
    // 开始封装json
    if (clientmsg->flags == 0)
        sprintf(send_json_buf, "{\"flag\":%d,\"buf\":\"%s\"}", clientmsg->flags, get_infomsg_err());
    else
        sprintf(send_json_buf, "{\"flag\":%d,\"buf\":\"\"}", clientmsg->flags);

    if ((ret = send(clinode->client_sockfd, send_json_buf, strlen(send_json_buf), 0)) == -1)
    {
        ERR_PRIENT("send error");
        return -1;
    }

    return clientmsg->flags == 1 ? 0 : -1;
}
